package com.isesol.ime.service;

import com.github.pagehelper.*;
import com.isesol.arch.common.service.*;
import com.isesol.arch.common.utils.*;
import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import com.isesol.ime.validator.param.*;
import lombok.extern.slf4j.*;
import org.springframework.stereotype.*;

import javax.annotation.*;
import java.util.*;

/**
 * 机床参数
 */
@Component
@Slf4j
public class ParamService extends BaseService {

	@Resource
	ParamMapper paramMapper;

	@Resource
	CategoryMapper categoryMapper;

	@Resource
	MessageMapper messageMapper;

	@Resource
	MsgParamsMapper msgParamsMapper;

	@Resource
	ParamVersionMapper paramVersionMapper;

	public ParamDO assemblyParam(ParamDTO paramDTO) {

		ParamDO paramDO = new ParamDO();

		paramDO.setStatus(StatusEnum.ON.getStatus());
		paramDO.setParamId(paramDTO.getParamId());
		paramDO.setParamName(paramDTO.getParamName());
		paramDO.setName(paramDTO.getName());
		paramDO.setDefaultValue(paramDTO.getDefaultValue());
		paramDO.setMinValue(paramDTO.getMinValue());
		paramDO.setMaxValue(paramDTO.getMaxValue());
		paramDO.setMemo(paramDTO.getMemo());
		paramDO.setParamUnit(paramDTO.getParamUnit());

		return paramDO;
	}

	/**
	 * 机床参数新增
	 *
	 * @param paramDTO
	 * @return
	 */
	public void create(ParamDTO paramDTO) {

		FluentValidator.checkAll().failOver()
				.on(paramDTO, SpringContextHolder.getBean(ParamCreateValidator.class))
				.doValidate();

		ParamDO paramDO = assemblyParam(paramDTO);
		paramDO.setCategoryId(Long.parseLong(paramDTO.getCategoryId()));
		paramDO.setParamVersion(paramDTO.getParamVersionId());
		paramMapper.insert(paramDO);
		paramDTO.setId(paramDO.getId());
	}

	/**
	 * 机床参数修改
	 *
	 * @param paramDTO
	 * @return
	 */
	public void update(ParamDTO paramDTO) {

		FluentValidator.checkAll().failOver()
				.on(paramDTO, SpringContextHolder.getBean(ParamUpdateValidator.class))
				.doValidate();

		ParamDO paramDO = paramMapper.getById(paramDTO.getId());
		paramDO.setParamName(paramDTO.getParamName());
		paramDO.setName(paramDTO.getName());
		paramDO.setCategoryId(Long.parseLong(paramDTO.getCategoryId()));
		paramDO.setParamUnit(paramDTO.getParamUnit());
		paramDO.setDefaultValue(paramDTO.getDefaultValue());
		paramDO.setMinValue(paramDTO.getMinValue());
		paramDO.setMaxValue(paramDTO.getMaxValue());
		paramDO.setMemo(paramDTO.getMemo());
		paramDO.setParamId(paramDTO.getParamId());
		paramDO.setParamVersion(paramDTO.getParamVersionId());

		paramMapper.update(paramDO);

	}

	/**
	 * 机床参数删除，逻辑删除
	 *
	 * @param paramDTO
	 * @return
	 */
	public void del(ParamDTO paramDTO) {

		FluentValidator.checkAll().failOver()
				.on(paramDTO, SpringContextHolder.getBean(ParamDeleteValidator.class))
				.doValidate();

		ParamDO paramDO = paramMapper.getById(paramDTO.getId());
		paramDO.setStatus(StatusEnum.OFF.getStatus());
		paramMapper.update(paramDO);

	}

	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public ParamDTO findById(long id) {

		return paramMapper.findById(id);

	}

	/**
	 * 分页
	 *
	 * @param paramDTO 参数DTO
	 * @param page     页数
	 * @param rows     每页显示数量
	 * @return
	 */
	public PageInfo<ParamDTO> find(ParamDTO paramDTO, int page, int rows) {

		PageHelper.startPage(page, rows);

		List<com.isesol.ime.dal.model.dto.ParamDTO> paramDTOList = paramMapper.find(paramDTO.getParamId(), paramDTO.getParamName(),
				paramDTO.getName(),
				String.valueOf(paramDTO.getCategoryId()), paramDTO
						.getParamVersion());

		return new PageInfo(paramDTOList);
	}

}
